import operator

import jsonpath

from  common.recordlog import  logs
from  common.read_yaml import get_testcase_yaml





class  Assrtions:

    def  contains(self,value,responce,status_code):
        #断言状态标识，0代表成功，其他代表失败
        flag=0
        for assert_key,aseert_value in value.items():
            print(assert_key)#aseert_value)
            if assert_key =='status_codes':
                if  aseert_value != status_code:
                    flag=flag+1
                    logs.error('contanins断言失败，接口返回码[%s]不等于[%s]'%(status_code,aseert_value))
            else:
                resp_list=jsonpath.jsonpath(responce,'$..%s'%assert_key)
                '''jsonpath提取器返回的是列表类型，所以下面要做个索引的处理'''
                if  isinstance(resp_list[0],str):
                     resp_list=''.join(resp_list)
                if  resp_list:
                     if  aseert_value in  resp_list:
                         logs.info('断言成功')
                     else:
                         flag=flag+1
                         logs.error(f'预期结果为{aseert_value}\n 实际结果为{resp_list}','响应断言结果失败')
        return   flag

    def  equal_assert(self,value,responce):
        flag=0
        res_lst=[]
        if  isinstance(value,dict)  and   isinstance(responce,dict):
             for  res in  responce:
                 #print(res)
                 print(list(value.keys())[0])
                 if  list(value.keys())[0] != res:
                     res_lst.append(res)
             for  rl  in res_lst:
                 del  responce[rl]
             print(f'实际结果{responce}')
             eq_assert=operator.eq(responce,value)
             if  eq_assert ==True:
                 logs.info(f'相等断言成功：接口的实际结果为{responce},等于预期结果{str(value)}')
             #print(res_lst)
        else:
            logs.error('相等断言失败-数据类型错误')

        return   flag

        '''相等模式'''


    def  not_equal_assert(self,value,responce):

        '''不相等模式'''


    def  assert_result(self,expected,responce,status_code):

        all_flag=0
        try:
            for  yq  in expected:
                for  key,value in  yq.items():
                    if  key == 'contains':
                        flag=self.contains(value,responce,status_code)
                        all_flag=flag+all_flag
                    elif  key == 'eq':
                        flag=self.equal_assert(value,responce)
                        all_flag=flag+all_flag
            assert   all_flag==0
            logs.info('测试成功！')
        except Exception as e:
            logs.error('测试失败！')
            logs.error(f'异常信息:{e}')
            assert   all_flag==0







if  __name__=='__main__':
    data=get_testcase_yaml('../testcase/login/Login.yaml')[0]
    responce={"api_info":"today:21 max:10000 all[90=21+33+36];expires:2030-12-31","cache":0,"error_code":"0000","goodsList":[{"goodsId":"18382788819","goods_count":"233","goods_image":"https://omsproductionimg.yangkeduo.com/images/2017-12-12/bcf848aa71c6389607ae7a84b70f1543.jpeg","goods_name":"【2件套】套装秋冬新款仿獭兔毛钉珠皮草毛毛短外套加厚大衣女装","original_price":"","unit_price":"￥99.00"},{"goodsId":"33809635011","goods_count":"521","goods_image":"https://omsproductionimg.yangkeduo.com/images/2017-12-12/176019babfdecffa1d9f98f40b7e99b4.jpeg","goods_name":"好奇小森林心钻装纸尿裤M22拉拉裤L18/XL14超薄透气裤型尿不湿 1件装","original_price":"","unit_price":"￥108.00"},{"goodsId":"56996760797","goods_count":"1181","goods_image":"https://omsproductionimg.yangkeduo.com/images/2017-12-12/efb5db42397550bffd3211ca6f197498.jpeg","goods_name":"冻干鸡小胸整块增肥营养发腮狗狗零食新手养猫零食幼猫零食100g","original_price":"","unit_price":"￥17.80"},{"goodsId":"82193785267","goods_count":"3000+","goods_image":"https://omsproductionimg.yangkeduo.com/images/2017-12-12/efb5db42397550bffd3211ca6f197498.jpeg","goods_name":"【自营】ISB伊珊娜意大利水果系列宠物犬猫沐浴露除臭香波护毛素","original_price":"","unit_price":"￥650.00"},{"goodsId":"74190550836","goods_count":"1000+","goods_image":"https://omsproductionimg.yangkeduo.com/images/2017-12-12/efb5db42397550bffd3211ca6f197498.jpeg","goods_name":"【新品零0CM嵌入式】海尔电冰箱410L家用法式四门多门官方正品","original_price":"","unit_price":"￥5746.00"}],"reason":"","request_id":"request_id","secache":"c98b29872e8a4b28859db207944ba817","secache_date":"2025-01-07 07:09:11","secache_time":1736204951000,"translate_language":"zh-CN"}
    value=[{'contains': {'error_code': '0000'}}]
    #print(value)
    A=Assrtions()
    print(value)
    A.assert_result(value,responce=responce,status_code=200)






